package cn.autumnorange.app.user.consumer.authorization.system.userDetails;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Objects;

public class UserDetailsImpl extends AbstractUserDto implements UserDetails {
  private static final String PASSWORD = "password";
  private Collection<? extends GrantedAuthority> authorities;
  private boolean accountNonExpired;
  private boolean accountNonLocked;
  private boolean credentialsNonExpired;
  private boolean enabled;

  private UserDetailsImpl() {}

  public UserDetailsImpl(
      Long id,
      String username,
      String password,
      Collection<? extends GrantedAuthority> authorities,
      boolean accountNonExpired,
      boolean accountNonLocked,
      boolean credentialsNonExpired,
      boolean enabled) {
    super.setId(id);
    super.setName(username);
    super.setPassword(password);
    this.authorities = authorities;
    this.accountNonExpired = accountNonExpired;
    this.accountNonLocked = accountNonLocked;
    this.credentialsNonExpired = credentialsNonExpired;
    this.enabled = enabled;
  }

  //    public static UserDetails createUserDetailByMap(Map jwtTokenMap) {
  //        List<Map> roleMapList = (List<Map>)
  // jwtTokenMap.get(JwttokenconfigEnum.roles.getValue());
  //        HashSet<SimpleGrantedAuthority> authorities = new HashSet<SimpleGrantedAuthority>();
  //        for (Map roleMap : roleMapList) {
  //            SimpleGrantedAuthority simpleGrantedAuthority =
  //                    new SimpleGrantedAuthority(roleMap.get("authority").toString());
  //            authorities.add(simpleGrantedAuthority);
  //        }
  //        UserDetailsImpl userDetails =
  //                new UserDetailsImpl(
  //
  // Long.parseLong(jwtTokenMap.get(JwttokenconfigEnum.jti.getValue()).toString()),
  //
  // StringUtils.toStringByObject(jwtTokenMap.get(JwttokenconfigEnum.sub.getValue())),
  //                        StringUtils.toStringByObject(jwtTokenMap.get(PASSWORD)),
  //                        authorities,
  //                        true,
  //                        true,
  //                        true,
  //                        true);
  //        return userDetails;
  //    }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
  }

  @Override
  public String getPassword() {
    return super.getPassword();
  }

  @Override
  public String getUsername() {
    return super.getName();
  }

  @Override
  public boolean isAccountNonExpired() {
    return accountNonExpired;
  }

  @Override
  public boolean isAccountNonLocked() {
    return accountNonLocked;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return credentialsNonExpired;
  }

  @Override
  public boolean isEnabled() {
    return enabled;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof UserDetailsImpl)) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    UserDetailsImpl that = (UserDetailsImpl) o;
    return accountNonExpired == that.accountNonExpired
        && accountNonLocked == that.accountNonLocked
        && credentialsNonExpired == that.credentialsNonExpired
        && enabled == that.enabled
        && Objects.equals(authorities, that.authorities);
  }

  @Override
  public int hashCode() {

    return Objects.hash(
        super.hashCode(),
        authorities,
        accountNonExpired,
        accountNonLocked,
        credentialsNonExpired,
        enabled);
  }
}
